package com.ficus.road.maintain.service;

import com.ficus.road.maintain.core.infrastructure.mongo.MongoDbConfig;
import com.ficus.road.maintain.core.model.DistrictVO;
import com.ficus.road.maintain.core.model.RoadInfo;
import com.ficus.road.maintain.core.model.RoadSection;
import com.ficus.road.maintain.core.model.smart.District;
import com.ficus.road.maintain.dao.CompanyDao;
import com.ficus.road.maintain.dao.HitDao;
import com.ficus.road.maintain.dao.RoadInfoDao;
import com.ficus.road.maintain.dao.RoadSectionDao;
import com.ficus.road.maintain.dao.smart.DistrictDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2021/8/16 下午7:39
 */
@Service
@Slf4j
public class DistrictService {

    @Autowired
    private RoadSectionDao roadSectionDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private HitDao hitDao;

    @Autowired
    private MongoDbConfig mongoDbConfig;

    @Autowired
    private DistrictDao districtDao;

    @Autowired
    private RoadInfoDao roadInfoDao;

    public List<DistrictVO> queryDistrict(String districtId) {
        ArrayList<DistrictVO> districtVOS = new ArrayList<>();
        Query query = new Query(Criteria.where("district_id").is(districtId));

        List<RoadInfo> roadInfoList = roadInfoDao.findByQuery(query);
        List<District> allDistrict = districtDao.findAll();
        Map<String, District> districtMap = allDistrict.stream().collect(Collectors.toMap(District::getId, item -> item));
        if (ObjectUtils.isNotEmpty(roadInfoList)) {
            roadInfoList.forEach(
                    i -> {
                        District district = districtMap.get(i.getDistrictId());
                        String pci = null;
                        String pqi = null;
                        if (district != null) {
                            pci = district.getPci();
                            pqi = district.getPqi();
                        }
                        DistrictVO districtVO = DistrictVO.builder()
                                .roadName(i.getRoadName())
                                .secNum(i.getSecNum())
                                .roadType(i.getRoadType().toString())
                                .secLen(ObjectUtils.isEmpty(i.getSecLen()) ? i.getSecLen() : String.format("%.1f", Double.parseDouble(i.getSecLen())))
                                .secArea(ObjectUtils.isEmpty(i.getSecArea()) ? i.getSecArea() : String.format("%.1f", Double.parseDouble(i.getSecArea())))
                                .pqi(pqi)
                                .pci(pci)
                                .startStake(i.getStartStake())
                                .endStake(i.getEndStake())
                                .company(i.getCompanyName())
                                .coordinates(i.getCoordinates())
                                .build();
                        districtVOS.add(districtVO);
                    }
            );
        }
//        ArrayList<DistrictVO> districtVOS = new ArrayList<>();
//        Query query = new Query(Criteria.where("district_id").is(districtId));
//        List<RoadSection> roadSectionList = roadSectionDao.findHitBySmart(query);
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.MONTH, -12);
//        Long beginDate = calendar.getTimeInMillis() / 1000;
//        if (ObjectUtils.isNotEmpty(roadSectionList)) {
//            roadSectionList.stream().forEach(
//                    i -> {
//                        String diseaseDensity = calculateDiseaseDensity(i, beginDate);
//                        String coordinates = i.getWgs84Location();
//                        List<Coordinate> coordinateList = JSONArray.parseArray(coordinates, Coordinate.class);
//                        DistrictVO districtVO = DistrictVO.builder()
//                                .roadName(i.getRoadName())
//                                .secNum(i.getSecNum())
//                                .roadType(RoadTypeEnum.DIRECT.toString())
//                                .secLen(ObjectUtils.isEmpty(i.getSecLen()) ? i.getSecLen() : String.format("%.1f", Double.parseDouble(i.getSecLen())))
//                                .secArea(ObjectUtils.isEmpty(i.getSecArea()) ? i.getSecArea() : String.format("%.1f", Double.parseDouble(i.getSecArea())))
//                                .pqi(i.getPqi())
//                                .startStake(i.getStartStake())
//                                .endStake(i.getEndStake())
//                                .company(i.getCompany())
//                                .coordinates(coordinateList)
//                                .diseaseDensity(diseaseDensity)
//                                .build();
//                        districtVOS.add(districtVO);
//                    }
//            );
//        }
//        List<DistrictRoad> districtRoadList = districtRoadDao.findHitBySmart(query);
//        if (ObjectUtils.isNotEmpty(districtRoadList)) {
//            districtRoadList.stream().forEach(
//                    i -> {
//                        List<List<Double>> points = i.getPoints();
//                        List<Coordinate> coordinateList = points.stream().map(item -> new Coordinate(String.valueOf(item.get(0)), String.valueOf(item.get(1)))).collect(Collectors.toList());
////                        List<Coordinate> coordinateList = JSONArray.parseArray(coordinates, Coordinate.class);
//                        DistrictVO districtVO = DistrictVO.builder()
//                                .roadName(i.getRoadName())
//                                .secNum(i.getRoadId())
//                                .roadType(RoadTypeEnum.ROAD.toString())
//                                .secLen(ObjectUtils.isEmpty(i.getLength()) ? i.getLength() : String.format("%.1f", Double.parseDouble(i.getLength()) / 1000))
////                                .secArea(ObjectUtils.isEmpty(i.getSecArea()) ? i.getSecArea() : String.format("%.1f", Double.parseDouble(i.getSecArea())))
////                                .pqi(i.getPqi())
////                                .startStake(i.getStartStake())
////                                .endStake(i.getEndStake())
//                                .company(roadSectionList.get(0).getCompany())
//                                .coordinates(coordinateList)
//                                .build();
//                        districtVOS.add(districtVO);
//                    }
//            );
//        }
        return districtVOS;
    }

    //第三步：查询区域信息的时候，统计病害密度
    public String calculateDiseaseDensity(RoadSection roadSection, Long beginDate) {
        Query query = new Query(Criteria.where("attrs.road_section").is(roadSection.getId()).and("hit_time").gte(beginDate));
        long count = hitDao.count(query);
        String secLen = roadSection.getSecLen();
        if (ObjectUtils.isNotEmpty(roadSection.getSecLen())) {
            double len = Double.parseDouble(secLen);
            String diseaseDensity = String.format("%.2f", count / len);
            return diseaseDensity;
        }
        return null;
    }
}
